package pzl.com.drawrelated.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-21]
 */
public class V_LinearGradient extends View {
    int w;
    int h;
    Paint paint;
    LinearGradient linearGradientCLAMPsmall;
    LinearGradient linearGradientCLAMPbig;
    LinearGradient linearGradientCLAMMultiCoclor;

    public V_LinearGradient(Context context) {
        super(context);
    }

    public V_LinearGradient(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        linearGradientCLAMPsmall = new LinearGradient(0, 0, 100, 100, Color.RED, Color.parseColor("#d11449e7"), Shader.TileMode.CLAMP);
        linearGradientCLAMPbig = new LinearGradient(0, 0, 700, 700, Color.GREEN, Color.parseColor("#d15c329f"), Shader.TileMode.CLAMP);
    }

    public V_LinearGradient(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        w = getWidth();
        h = getHeight();
        linearGradientCLAMMultiCoclor = new LinearGradient(0, 0, w, 400, new int[]{
                Color.parseColor("#f2312e"), Color.parseColor("#2ef2cb"), Color.parseColor("#d15c329f"), Color.parseColor("#e6663b")}, null, Shader.TileMode.CLAMP);
    }

    /**
     * 三个TileMode可选CLAMP、REPEAT、MIRROR
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setShader(linearGradientCLAMPsmall);
        Rect rect1 = new Rect();
        rect1.top = 20;
        rect1.left = 20;
        rect1.right = w - 20;
        rect1.bottom = rect1.top + 400;
        canvas.drawRect(rect1, paint);

        paint.setShader(linearGradientCLAMPbig);
        Rect rect2 = new Rect();
        rect2.top = rect1.bottom + 20;
        rect2.left = 300;
        rect2.right = w - 200;
        rect2.bottom = rect2.top + 100;
        canvas.drawRect(rect2, paint);

        paint.setShader(linearGradientCLAMMultiCoclor);
        Rect rect3 = new Rect();
        rect3.top = rect2.bottom + 20;
        rect3.left = 0;
        rect3.right = w;
        rect3.bottom = rect3.top + 400;
        canvas.drawRect(rect3, paint);


    }


}
